package com.javaking.clanteam.hvz.utils;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.javaking.clanteam.hvz.R;
import com.javaking.clanteam.hvz.sql.HVZUser;
import com.javaking.clanteam.hvz.sql.SQLRelay;

public class ModeratorApprovalActivity extends BarcodeScanActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (savedInstanceState==null) savedInstanceState = new Bundle();
		savedInstanceState.putBoolean("ignore", true);
		super.onCreate(savedInstanceState);
	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Approval needed");
    	builder.setMessage("You need moderator approval for this action.\n" +
    			"Please scan a mod's id card");
    	builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				confirmMod();
			}
		});
    	builder.show();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode) {
		case IntentIntegrator.REQUEST_CODE:
			try {
				// Try to get the barcode from the scan
				String barcode = IntentIntegrator.parseActivityResult(requestCode, resultCode, data).getContents();
				checkForMod(barcode);
			} catch (Exception e) {
				Log.e("hvz", e.toString());
			}
			break;
		}
	}

	private void checkForMod(String barcode) {
		HVZUser user = SQLRelay.readUser(barcode);
		if(user.getUsertype().equals("MODERATOR")) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Accepted");
			builder.setMessage("Proceeding with requested action");
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					setResult(RESULT_OK, new Intent().putExtra("approved", true));
					finish();
				}
			});
			builder.show();
		} else {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("FAILURE");
			builder.setMessage("The user scanned was not a moderator.");
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					setResult(RESULT_OK, new Intent().putExtra("approved", false));
					finish();
				}
			});
			builder.show();
		}
	}
	
    protected void confirmMod() {
    	setContentView(R.layout.scan);
        setTitle(R.string.scanOrInput);
    }
    
    @Override
    public void manualKey(View v) {
    	super.manualKey(v);
    	
    	EditText manualBarcodeEdit = (EditText)findViewById(R.id.manualBarcodeEdit);
		manualBarcodeEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(actionId==EditorInfo.IME_NULL||actionId==EditorInfo.IME_ACTION_DONE)
				{
					checkForMod(v.getText().toString());
					return true;
				}
				return false;
			}
		});
    }
    
}
